<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>08 定位 | HasakWebBlog</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="hasak">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.a5a0d673.css" as="style"><link rel="preload" href="/assets/js/app.c7915c77.js" as="script"><link rel="preload" href="/assets/js/3.f80332a6.js" as="script"><link rel="preload" href="/assets/js/1.5cffa4a1.js" as="script"><link rel="preload" href="/assets/js/18.901e5492.js" as="script"><link rel="prefetch" href="/assets/js/10.02ae8499.js"><link rel="prefetch" href="/assets/js/11.c5238d8f.js"><link rel="prefetch" href="/assets/js/12.be21fd80.js"><link rel="prefetch" href="/assets/js/13.bd684308.js"><link rel="prefetch" href="/assets/js/14.b5f9fc77.js"><link rel="prefetch" href="/assets/js/15.318683dc.js"><link rel="prefetch" href="/assets/js/16.74f9d94e.js"><link rel="prefetch" href="/assets/js/17.cd1e6d69.js"><link rel="prefetch" href="/assets/js/19.417d2070.js"><link rel="prefetch" href="/assets/js/20.39ec66a2.js"><link rel="prefetch" href="/assets/js/21.fbf1aaf8.js"><link rel="prefetch" href="/assets/js/22.736aeb98.js"><link rel="prefetch" href="/assets/js/23.abcdc310.js"><link rel="prefetch" href="/assets/js/24.26658596.js"><link rel="prefetch" href="/assets/js/25.40bdc691.js"><link rel="prefetch" href="/assets/js/26.29ac009b.js"><link rel="prefetch" href="/assets/js/27.edc6afbf.js"><link rel="prefetch" href="/assets/js/28.b9c1065e.js"><link rel="prefetch" href="/assets/js/29.b46f8f9e.js"><link rel="prefetch" href="/assets/js/30.6839ae7e.js"><link rel="prefetch" href="/assets/js/31.85913f4e.js"><link rel="prefetch" href="/assets/js/32.098b21df.js"><link rel="prefetch" href="/assets/js/33.faced894.js"><link rel="prefetch" href="/assets/js/34.239b19e7.js"><link rel="prefetch" href="/assets/js/35.5c8a8406.js"><link rel="prefetch" href="/assets/js/36.46f0ebac.js"><link rel="prefetch" href="/assets/js/37.2456f410.js"><link rel="prefetch" href="/assets/js/38.b76624ee.js"><link rel="prefetch" href="/assets/js/39.64ecef14.js"><link rel="prefetch" href="/assets/js/4.7d7a493e.js"><link rel="prefetch" href="/assets/js/40.a1304a44.js"><link rel="prefetch" href="/assets/js/41.f98208ce.js"><link rel="prefetch" href="/assets/js/42.f0be4a4c.js"><link rel="prefetch" href="/assets/js/43.1a46f61e.js"><link rel="prefetch" href="/assets/js/44.d579b00f.js"><link rel="prefetch" href="/assets/js/45.8e8ea47a.js"><link rel="prefetch" href="/assets/js/46.1b194721.js"><link rel="prefetch" href="/assets/js/47.994c7d71.js"><link rel="prefetch" href="/assets/js/48.f17a641c.js"><link rel="prefetch" href="/assets/js/49.0d9ea06f.js"><link rel="prefetch" href="/assets/js/5.176296fc.js"><link rel="prefetch" href="/assets/js/50.e36885a9.js"><link rel="prefetch" href="/assets/js/51.64538ee2.js"><link rel="prefetch" href="/assets/js/52.1fd24c2e.js"><link rel="prefetch" href="/assets/js/53.967b1458.js"><link rel="prefetch" href="/assets/js/54.05b15a98.js"><link rel="prefetch" href="/assets/js/55.3c78b9d4.js"><link rel="prefetch" href="/assets/js/56.8d130c30.js"><link rel="prefetch" href="/assets/js/57.0008c022.js"><link rel="prefetch" href="/assets/js/58.4b49a382.js"><link rel="prefetch" href="/assets/js/59.8bb182dc.js"><link rel="prefetch" href="/assets/js/6.90835ce9.js"><link rel="prefetch" href="/assets/js/60.93235335.js"><link rel="prefetch" href="/assets/js/61.78370acd.js"><link rel="prefetch" href="/assets/js/62.1144f314.js"><link rel="prefetch" href="/assets/js/63.7fb72cd2.js"><link rel="prefetch" href="/assets/js/64.de8ba1be.js"><link rel="prefetch" href="/assets/js/65.cefa274d.js"><link rel="prefetch" href="/assets/js/66.32ad44bf.js"><link rel="prefetch" href="/assets/js/67.558c356c.js"><link rel="prefetch" href="/assets/js/68.15a3af19.js"><link rel="prefetch" href="/assets/js/69.b1b26c0a.js"><link rel="prefetch" href="/assets/js/7.2c075495.js"><link rel="prefetch" href="/assets/js/70.cb9b8e94.js"><link rel="prefetch" href="/assets/js/71.e371986a.js"><link rel="prefetch" href="/assets/js/72.a8af9117.js"><link rel="prefetch" href="/assets/js/73.dcc5a1af.js"><link rel="prefetch" href="/assets/js/74.803a909b.js"><link rel="prefetch" href="/assets/js/75.8129f8f6.js"><link rel="prefetch" href="/assets/js/76.ffcb07fc.js"><link rel="prefetch" href="/assets/js/8.9b4cfefe.js"><link rel="prefetch" href="/assets/js/9.e529cf11.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a5a0d673.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-7dd95ae2><div data-v-7dd95ae2><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>HasakWebBlog</h3> <p class="description" data-v-59e6cb88>hasak</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>hasak</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div class="hide" data-v-7dd95ae2><header class="navbar" data-v-7dd95ae2><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="HasakWebBlog" class="logo"> <span class="site-name">HasakWebBlog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端学习
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/HTML5&amp;CSS3/" class="nav-link"><i class="undefined"></i>
  HTML5&amp;CSS3
</a></li><li class="dropdown-item"><!----> <a href="/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node/" class="nav-link"><i class="undefined"></i>
  Node
</a></li><li class="dropdown-item"><!----> <a href="/categories/VUE/" class="nav-link"><i class="undefined"></i>
  VUE
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/hasakxu" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-7dd95ae2></div> <aside class="sidebar" data-v-7dd95ae2><div class="personal-info-wrapper" data-v-1fad0c41 data-v-7dd95ae2><img src="/dog.png" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    hasak
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>66</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>5</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端学习
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/HTML5&amp;CSS3/" class="nav-link"><i class="undefined"></i>
  HTML5&amp;CSS3
</a></li><li class="dropdown-item"><!----> <a href="/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node/" class="nav-link"><i class="undefined"></i>
  Node
</a></li><li class="dropdown-item"><!----> <a href="/categories/VUE/" class="nav-link"><i class="undefined"></i>
  VUE
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/hasakxu" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>HTML5&amp;CSS3 学习</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blogs/基础/html/20221107html01.html" class="sidebar-link">01 字符实体与语义标签</a></li><li><a href="/blogs/基础/html/20221107html02.html" class="sidebar-link">02 CSS语法与选择器</a></li><li><a href="/blogs/基础/html/20221110html03.html" class="sidebar-link">03 样式继承与其他概念</a></li><li><a href="/blogs/基础/html/20221110html04.html" class="sidebar-link">04 盒模型</a></li><li><a href="/blogs/基础/html/20221110html05.html" class="sidebar-link">05 盒模型补充</a></li><li><a href="/blogs/基础/html/20221113html06.html" class="sidebar-link">06 浮动</a></li><li><a href="/blogs/基础/html/20221113html07.html" class="sidebar-link">07 高度塌陷与BFC</a></li><li><a href="/blogs/基础/html/20221113html08.html" class="active sidebar-link">08 定位</a></li><li><a href="/blogs/基础/html/20221115html09.html" class="sidebar-link">09 字体</a></li><li><a href="/blogs/基础/html/20221115html10.html" class="sidebar-link">10 背景</a></li><li><a href="/blogs/基础/html/20221115html11.html" class="sidebar-link">11 雪碧图和渐变</a></li><li><a href="/blogs/基础/html/20221115html12.html" class="sidebar-link">12 表格</a></li><li><a href="/blogs/基础/html/20221115html13.html" class="sidebar-link">13 过渡与动画</a></li><li><a href="/blogs/基础/html/20221115html14.html" class="sidebar-link">14 变形：平移、旋转与缩放</a></li><li><a href="/blogs/基础/html/20221115html15.html" class="sidebar-link">15 LESS简介</a></li><li><a href="/blogs/基础/html/20221115html16.html" class="sidebar-link">16 弹性盒简介</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>08 定位</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>hasak</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div data-v-7dd95ae2><div data-v-7dd95ae2><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">08 定位</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>hasak</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>6/18/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>CSS</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="_08-定位"><a href="#_08-定位" class="header-anchor">#</a> 08 定位</h1> <p>**定位（position）**是一种更加高级的布局手段，通过定位可以将元素摆放到页面的任意位置</p> <p>使用<code>position</code>属性来设置定位</p> <table><thead><tr><th style="text-align:center;">可选值</th> <th style="text-align:center;">含义</th></tr></thead> <tbody><tr><td style="text-align:center;"><strong><code>static</code></strong></td> <td style="text-align:center;">不开启定位，元素是静止的，默认值</td></tr> <tr><td style="text-align:center;"><strong><code>relative</code></strong></td> <td style="text-align:center;">开启元素的相对定位</td></tr> <tr><td style="text-align:center;"><strong><code>absolute</code></strong></td> <td style="text-align:center;">开启元素的绝对定位</td></tr> <tr><td style="text-align:center;"><strong><code>fixed</code></strong></td> <td style="text-align:center;">开启元素的固定定位</td></tr> <tr><td style="text-align:center;"><strong><code>sticky</code></strong></td> <td style="text-align:center;">开启元素的粘滞定位</td></tr></tbody></table> <h2 id="_1-相对定位-relative"><a href="#_1-相对定位-relative" class="header-anchor">#</a> 1. 相对定位 relative</h2> <p>当元素的<code>position</code>属性值设置为<code>relative</code>时，则开启了元素的相对定位</p> <ul><li>需要设置偏移量来产生变化</li> <li>使用相对定位后，只会移动自身的布局位置，而不会对已存在的其他元素产生任何影响</li></ul> <h3 id="偏移量-offset"><a href="#偏移量-offset" class="header-anchor">#</a> 偏移量（offset）</h3> <p>当元素开启相对定位后，可以通过偏移量来设置元素的位置</p> <table><thead><tr><th style="text-align:center;">offset属性</th> <th style="text-align:center;">含义</th></tr></thead> <tbody><tr><td style="text-align:center;"><strong><code>top</code></strong></td> <td style="text-align:center;">定位元素和定位位置的上边距离</td></tr> <tr><td style="text-align:center;"><strong><code>bottom</code></strong></td> <td style="text-align:center;">定位元素和定位位置的下边距离</td></tr> <tr><td style="text-align:center;"><strong><code>left</code></strong></td> <td style="text-align:center;">定位元素和定位位置的左边距离</td></tr> <tr><td style="text-align:center;"><strong><code>right</code></strong></td> <td style="text-align:center;">定位元素和定位位置的右边距离</td></tr></tbody></table> <p>**<code>top</code>**和 **<code>bottom</code>**两个属性定位元素垂直方向的位置</p> <ul><li>**<code>top</code>**值越大，定位元素越靠下</li> <li>**<code>bottom</code>**值越大，定位元素越靠上</li></ul> <p>**<code>left</code>**和 **<code>right</code>**两个属性定位元素垂直方向的位置</p> <ul><li>**<code>left</code>**值越大，定位元素越靠右</li> <li>**<code>right</code>**值越大，定位元素越靠左</li></ul> <div class="language-CSS line-numbers-mode"><pre class="language-css"><code><span class="token selector">body</span> <span class="token punctuation">{</span>
    <span class="token property">font-size</span><span class="token punctuation">:</span>60px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.box1</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>#bfa<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.box2</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
    <span class="token property">position</span><span class="token punctuation">:</span>relative<span class="token punctuation">;</span>
    <span class="token property">bottom</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">left</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.box3</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>yellow<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

&lt;div class=<span class="token string">&quot;box1&quot;</span>&gt;1&lt;/div&gt;
&lt;div class=<span class="token string">&quot;box2&quot;</span>&gt;2&lt;/div&gt;
&lt;div class=<span class="token string">&quot;box3&quot;</span>&gt;3&lt;/div&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211131125510.png" alt="relative1" style="zoom:50%;"> <h3 id="特点"><a href="#特点" class="header-anchor">#</a> 特点：</h3> <ol><li>元素开启相对定位后，如果不设置偏移量元素，元素不会发生任何变化（这里不仅仅是位置）</li> <li>相对定位是==参照于元素在文档流的原始位置进行定位的==</li> <li>相对定位会==提升元素的层级==（表现为可以覆盖其他元素）</li> <li>相对定位==不会改变元素的性质==（即块元素还是块元素，行内还是行内）</li></ol> <div class="language-CSS line-numbers-mode"><pre class="language-css"><code><span class="token selector">.box2</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
    <span class="token property">position</span><span class="token punctuation">:</span>relative<span class="token punctuation">;</span>
    <span class="token property">bottom</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">left</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151427682.gif" alt="position1"></p> <p><strong>Q1：多个div都设置相对定位，他们的层级关系会怎么样？谁会被谁覆盖？</strong></p> <p>​			在没有设置层级或层级<code>z-index</code>设置相同值时，优先显示靠下的元素。</p> <p><strong>Q2:相对定位的第三个特点会提升元素的层级，是否就类似于浮动一样脱离了文档流</strong></p> <p>​			浮动脱离文档流，不再占据页面位置；相对定位仍然占据页面位置。</p> <p>参考浮动和相对定位的区别：</p> <ul><li>参考系不同：浮动的参考系时其父元素；相对定位是相对于自身</li> <li>可移动方向不同：浮动只能左右移动；相对定位可以上下左右移动</li> <li>影响不同：浮动会影响页面布局（包括下方元素位置影响和高度塌陷问题）；相对定位不影响页面布局</li> <li>性质不同：浮动会改变元素的性质（使得不再独占一行，宽高都会被内容撑开）；相对定位则不改变元素性质</li> <li>文字环绕：浮动不会覆盖文字；相对定位可以覆盖文字</li> <li>此外，==浮动和相对定位都不会从父元素中移出==</li></ul> <p><strong>Q3:相对定位不会改变元素的性质，但原始位置是否不再独占一行</strong></p> <p>​			其原始位置依然独占一行</p> <p><strong>Q4:相对定位的第四个特点意味着行内元素也可以使用相对定位吗？</strong></p> <p>​			是的</p> <h2 id="_2-绝对定位"><a href="#_2-绝对定位" class="header-anchor">#</a> 2. 绝对定位</h2> <p>当元素的<code>position</code>属性设置为<code>absolute</code>时，则开启了元素的绝对属性</p> <h3 id="特点-2"><a href="#特点-2" class="header-anchor">#</a> 特点</h3> <ol><li>开启了绝对定位后，如果不设置偏移值，元素的位置不会发生变化（只是位置不变）</li> <li>绝对定位是==相对于其他包含块进行定位的==</li> <li>绝对定位会，元素会提升一个等级</li> <li>开启绝对定位后，<strong>元素会从文档流中</strong></li> <li>绝对定位会==改变元素的性质：行内变块，块的宽高默认被内容撑开==</li></ol> <div class="language-CSS line-numbers-mode"><pre class="language-css"><code><span class="token selector">body</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>#bfa<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.box2</span> <span class="token punctuation">{</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
    <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.box3</span> <span class="token punctuation">{</span>
    <span class="token property">width</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span>
    <span class="token property">height</span><span class="token punctuation">:</span>300px<span class="token punctuation">;</span>
    <span class="token property">background-color</span><span class="token punctuation">:</span>yellow<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

&lt;div class=<span class="token string">&quot;box1&quot;</span>&gt;1&lt;/div&gt;
&lt;div class=<span class="token string">&quot;box2&quot;</span>&gt;2&lt;/div&gt;
&lt;div class=<span class="token string">&quot;box3&quot;</span>&gt;3&lt;/div&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151457178.gif" alt="position2"></p> <h3 id="包含块-containing-block"><a href="#包含块-containing-block" class="header-anchor">#</a> 包含块（containing block）</h3> <p>正常情况下</p> <ul><li><strong>包含块就是离当前元素最近的开启定位的祖先块元素</strong></li> <li>如果所有的祖先元素都没有开启定位，那么<code>&lt;html&gt;</code>就是它的包含块</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--如果box1开启定位，则box2的包含块是box1，否则就是body--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
            
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  	<span class="token comment">&lt;!--如果box3开启定位，则em的包含块是box3，否则就是body--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span><span class="token punctuation">&gt;</span></span>
        	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>em</span><span class="token punctuation">&gt;</span></span>hello<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>em</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>2
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>3
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box4<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>4
            
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>不给box2、box3开定位，box4的包含块是<code>html</code></li> <li>只给box3开启定位后，box4的包含块是box3</li> <li>只给box2开启定位后，box4的包含块是box2</li> <li>给box2、box3都开启定位后，box4的包含块是box3</li></ul> <p>**注意：**开启定位，就是<code>position</code> 不是<code>static</code></p> <h3 id="水平方向的布局"><a href="#水平方向的布局" class="header-anchor">#</a> 水平方向的布局</h3> <p>之前写过，水平方向的布局等式：</p> <p><code>margin-left + border-left + padding-left + width + padding-right + border-right + margin-right = 其父元素的宽度</code></p> <p>当使用绝对定位时，添加<code>left</code> 和 <code>right</code> 两个值。</p> <p><code>left + margin-left + border-left + padding-left + width + padding-right + border-right + margin-right + right = 其父元素的宽度</code></p> <p>当发生过度约束时</p> <ul><li><strong>如果9个值中没有<code>auto</code>，则自动调整<code>right</code>值使得等式满足</strong></li> <li>如果9个值中有<code>auto</code>，则自动调整<code>auto</code>的值使得等式满足
<ul><li>可设置<code>auto</code>的值： <strong><code>margin-left</code> <code>margin-right</code> <code>width</code> <code>left</code> <code>right</code></strong></li> <li><code>left</code> 和<code>right</code>的值默认是<code>auto</code>,如果没有设置，会自动调整这俩值让等式成立，优先调<code>right</code></li></ul></li></ul> <p><strong>水平居中</strong></p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.box1</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>500px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>500px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>#bfa<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span>relative<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.box2</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
        
        <span class="token property">margin-left</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span>
        <span class="token property">margin-right</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">right</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151521790.png" alt="position3" style="zoom:50%;"> <h3 id="垂直方向的布局"><a href="#垂直方向的布局" class="header-anchor">#</a> 垂直方向的布局</h3> <p>垂直方向布局的等式要满足</p> <p><code>top + margin-top + border-top + padding-top + height + padding-bottom + border-bottom + margin-bottom + bottom = 其父元素的高度</code></p> <div class="language-CSS line-numbers-mode"><pre class="language-css"><code><span class="token selector">.box2</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
        
        <span class="token property">margin-top</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span>
        <span class="token property">margin-bottom</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">bottom</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151523724.png" alt="position4" style="zoom:50%;"> <p><strong>水平垂直居中</strong></p> <div class="language-CSS line-numbers-mode"><pre class="language-css"><code><span class="token selector">.box2</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span>
        
        <span class="token property">margin</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">bottom</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    	<span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">right</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151525410.png" alt="position5" style="zoom:50%;"> <h3 id="小结"><a href="#小结" class="header-anchor">#</a> 小结</h3> <ul><li><p>水平方向的布局等式：</p> <p><code>margin-left + border-left + padding-left + width + padding-right + border-right + margin-right = 其父元素的宽度</code></p></li> <li><p>垂直方向布局的等式：</p> <p><code>top + margin-top + border-top + padding-top + height + padding-bottom + border-bottom + margin-bottom + bottom = 其父元素的高度</code></p></li> <li><p>上述等式的过度约束规则盒模型要保持一致，只是在没有<code>auto</code>时，会自动调整 <code>top</code> <code>bottom</code> <code>left</code> <code>right</code></p></li></ul> <h2 id="_3-固定定位"><a href="#_3-固定定位" class="header-anchor">#</a> 3. 固定定位</h2> <p>将元素的<code>position</code> 属性改为 <code>fixed</code>, 则开启了元素的固定定位</p> <h3 id="特点-3"><a href="#特点-3" class="header-anchor">#</a> 特点</h3> <ul><li>固定定位也是一种绝对定位，所以大部分特点和绝对定位一样</li> <li>唯一不同的是：==固定定位永远参照浏览器的视口（viewport)进行定位，不随网页的滚动条滚动==</li></ul> <img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151529460.gif" alt="position6" style="zoom:67%;"> <img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151529086.gif" alt="position7" style="zoom:67%;"> <h2 id="_4-粘滞定位"><a href="#_4-粘滞定位" class="header-anchor">#</a> 4. 粘滞定位</h2> <p>将元素的<code>position</code>属性设置为<code>sticky</code>，则开启了元素的固定定位，兼容性不好</p> <h3 id="特点-4"><a href="#特点-4" class="header-anchor">#</a> 特点：</h3> <ul><li>元素到达某一个位置后固定
<ul><li>该元素根据文档流进行定位，即相对于包含块进行偏移</li> <li>偏移量不会影响任何其他元素的位置</li> <li>粘性元素总是”粘“到其最近的具有”滚动机制“的祖先元素 （当<code>overflow</code>为<code>hidden</code>、<code>scroll</code>、<code>auto</code>、<code>overlay</code>时创建，即使该祖先不是最近的实际滚动祖先</li></ul></li></ul> <h2 id="_5-几种定位的对比"><a href="#_5-几种定位的对比" class="header-anchor">#</a> 5. 几种定位的对比</h2> <table><thead><tr><th style="text-align:center;">定位方式</th> <th>是否不设置偏移量，<br>元素不会发生改变</th> <th>是否脱离文档流</th> <th>是否改变元素性质</th> <th>是否提升元素层级</th> <th>参考系</th></tr></thead> <tbody><tr><td style="text-align:center;">relative（相对定位）</td> <td>✔</td> <td>❌</td> <td>❌</td> <td>✔</td> <td>参照于元素在文档流中的位置</td></tr> <tr><td style="text-align:center;">absolute（绝对定位）</td> <td>❌</td> <td>✔</td> <td>✔</td> <td>✔</td> <td>参照于其包含块</td></tr> <tr><td style="text-align:center;">fixed（固定定位）</td> <td>❌</td> <td>✔</td> <td>✔</td> <td>✔</td> <td>参照于浏览器的视口</td></tr> <tr><td style="text-align:center;">sticky(粘滞定位)</td> <td>❌</td> <td>✔</td> <td>✔</td> <td>✔</td> <td>参照于浏览器的视口</td></tr></tbody></table> <h2 id="_6-补充-元素层级"><a href="#_6-补充-元素层级" class="header-anchor">#</a> 6. 补充：元素层级</h2> <p>对于开启了定位元素，可以通过<code>z-index</code>属性来指定元素的层级</p> <ul><li><code>z-index</code>需要一个整数作为参数，值越大，元素的层级越高；元素的层级越高，越优先显示</li> <li>如果元素的层级一样，则优先显示靠下的元素</li> <li>祖先的元素层级再高，也不会盖住后代元素</li></ul> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">div</span> <span class="token punctuation">{</span>
        <span class="token property">font-size</span><span class="token punctuation">:</span>40px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.box1</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>#bfa<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.box2</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>orange<span class="token punctuation">;</span> 
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">top</span><span class="token punctuation">:</span>50px<span class="token punctuation">;</span>
        <span class="token property">left</span><span class="token punctuation">:</span>50px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.box3</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>200px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>salmon<span class="token punctuation">;</span> 
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">top</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">left</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token selector">.box4</span> <span class="token punctuation">{</span>
        <span class="token property">width</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span>100px<span class="token punctuation">;</span>
        <span class="token property">background-color</span><span class="token punctuation">:</span>oskyblue<span class="token punctuation">;</span> 
        <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span>
        <span class="token property">bottom</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
        <span class="token property">right</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>3
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box4<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br></div></div><p><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211151543364.png" alt="position8"></p> <p><strong>Q:浮动也有层级概念吗？如果有，浮动和定位的层级关系是什么样的？</strong></p> <p>​		会出现以下的现象</p> <ul><li>给<code>float</code>设置<code>z-index</code>多大都没用，还是会被覆盖</li> <li>没有设置<code>z-index</code>或设置的<code>z-index</code>大小 ≥ 0时，浮动层级没有定位的层级高</li> <li>设置<code>z-index</code> &lt; 0 时，浮动层级可以定位的层级高</li></ul> <h3 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h3> <p>一般情况下：</p> <ul><li>页面的整体结构大多采用浮动、块进行布局</li> <li>页面某些模块结构一般采用定位进行微调</li></ul></div></section> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blogs/基础/html/20221113html07.html" class="prev">
          07 高度塌陷与BFC
        </a></span> <span class="next"><a href="/blogs/基础/html/20221115html09.html">
          09 字体
        </a></span></p></div> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:0;" data-v-b57cc07c data-v-7dd95ae2></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.c7915c77.js" defer></script><script src="/assets/js/3.f80332a6.js" defer></script><script src="/assets/js/1.5cffa4a1.js" defer></script><script src="/assets/js/18.901e5492.js" defer></script>
  </body>
</html>
